<template>
  <!-- 自定义表单验证
      validator:(rule,value,callback)=>{
         value：当前验证项的值
         callback:验证通过：callback()   验证不通过:callback(new Error('错误信息'))
      }
   -->

  <div>
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" />
      </el-form-item>
      <el-form-item label="确认密码" prop="password2">
        <el-input v-model="form.password2" />
      </el-form-item>
    </el-form>
    <el-button @click="submit">确定</el-button>

  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        password: '',
        password2: ''
      },
      rules: {
        password: [{ required: true, message: '必填', trigger: 'blur' }],
        password2: [{ required: true, message: '必填', trigger: 'blur' }, {
          validator: (rule, value, callback) => {
            // rule:规则
            // value:当前验证项的值
            // callback: 验证通过：callback()   验证不通过:callback(new Error('错误信息'))
            if (value !== this.form.password) {
              callback(new Error('确认密码要与密码相同'))
            } else {
              callback()
            }
          }, trigger: 'change'
        }]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(result => {
        console.log(result)
      })
    }
  }
}
</script>
<style>
</style>
